package com.yunchao.feature.lock.data.repository

import com.yunchao.feature.lock.domain.model.LockInfoModel
import com.yunchao.feature.lock.domain.model.LockLogModel
import com.yunchao.feature.lock.domain.repository.LockHandleRepository
import com.yunchao.library.lock.DeviceHelper
import com.yunchao.library.lock.LockEPCInfo
import timber.log.Timber

internal class LockHandleRepositoryImpl(private val deviceHelper: DeviceHelper) :
    LockHandleRepository {

    override suspend fun inventoryOnce(): Result<LockInfoModel> = runCatching {
        val lockEPCInfo = deviceHelper.inventoryOnce()
        LockInfoModel(lockEPCInfo = lockEPCInfo)
    }

    override suspend fun inventoryOnceAndLogCount(): Result<LockInfoModel> = runCatching {
        deviceHelper.readLockLogisticsLog()
    }.mapCatching {
        val lockCount = deviceHelper.readLockLogCount(it.first)
        LockInfoModel(
            lockEPCInfo = it.first,
            lockLogCount = lockCount,
            lockLogisticsCount = it.second.size
        )
    }

    /*
        override suspend fun inventoryOnceAndReadLog(): Result<LockInfoModel> = runCatching {
            deviceHelper.inventoryOnce()
        }.mapCatching { lockEpcInfo->
            val (lockLogCount, lastTwoLogs) = coroutineScope {
                val countDeferred = async { deviceHelper.readLockLogCount(lockEpcInfo) }
                val logsDeferred = async {
                    val count = countDeferred.await()
                    deviceHelper.readLockLog(lockEpcInfo, count - 1, 2)
                }
                countDeferred.await() to logsDeferred.await()
            }
            Triple(lastTwoLogs.first, lockLogCount, lastTwoLogs.second)
        }.mapCatching { result ->
            LockInfoModel(
                lockEPCInfo = result.first,
                lockLogCount = result.second,
                lockLogList = LockLogModel.getLogItems(result.third, result.first)
            )
        }*/

    override suspend fun openLock(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int,
        pageNumber: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.openLock(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId,
            pageNumber = pageNumber
        )
    }.mapCatching {
        LockInfoModel(lockEPCInfo = it, handleResult = true)
    }

    override suspend fun closeLock(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int,
        pageNumber: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.closeLock(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId,
            pageNumber = pageNumber
        )
    }.mapCatching {
        LockInfoModel(lockEPCInfo = it, handleResult = true)
    }

    override suspend fun openLockLogistics(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int,
        pageNumber: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.openLockWriteLogistics(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId,
            pageNumber = pageNumber
        )
    }.mapCatching {
        LockInfoModel(lockEPCInfo = it, handleResult = true)
    }

    override suspend fun closeLockLogistics(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int,
        pageNumber: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.closeLockWriteLogistics(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId,
            pageNumber = pageNumber
        )
    }.mapCatching {
        LockInfoModel(lockEPCInfo = it, handleResult = true)
    }

    override suspend fun recoveryLock(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.recoveryLock(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId
        )
    }.mapCatching {
        LockInfoModel(lockEPCInfo = it, handleResult = true)
    }

    override suspend fun cleanLock(
        lockEPCInfo: LockEPCInfo,
        deviceId: Int,
        operatorId: Int,
        chkUserId: Int,
        pageNumber: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.cleanLockLog(
            lockEPCInfo = lockEPCInfo,
            deviceId = deviceId,
            operatorId = operatorId,
            chkUserId = chkUserId,
            pageNumber = pageNumber
        )
    }.mapCatching {
        val lockLogCount = deviceHelper.readLockLogCount(it)
        LockInfoModel(lockEPCInfo = it, handleResult = true, lockLogCount = lockLogCount)
    }

    override suspend fun readLockLogistics(): Result<LockInfoModel> = runCatching {
        deviceHelper.readLockLogisticsLog()
    }.mapCatching {
        Timber.d("读取到物流信息: $it")
        LockInfoModel(
            lockEPCInfo = it.first,
            lockLogisticsCount = it.second.size,
            lockLogList = LockLogModel.getLogisticsItems(it.second)
        )
    }

    override suspend fun readLockLog(
        lockEPCInfo: LockEPCInfo,
        start: Int,
        number: Int
    ): Result<LockInfoModel> = runCatching {
        deviceHelper.readLockLog(lockEPCInfo, start, number)
    }.mapCatching {
        LockInfoModel(
            lockEPCInfo = it.first,
            lockLogList = LockLogModel.getLogItems(it.second, it.first)
        )
    }
}